package com.wind.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author wangwei
 * 
 */
public class DateUtil {

	public static final String twoSixFormat = "yyyy MM dd HH mm ss.s";

	public static final String eph_blhTimeFormat = "yyyy MM dd HH mm ss.sss    34.8157  114.8230    640206.0";

	public static final String normalTimeFormat = "yyyy-MM-dd HH:mm:ss";

	public static final String normalDateFormat = "yyyy-MM-dd";

	public static final String normalDateFormatMonth = "yyyy-MM";

	public static final String DateFormat_yyyyMMddHHmm = "yyyyMMddHHmm";
	public static final String DateFormat_yyyyMMddHHmmss = "yyyyMMddHHmmss";
	
	public static final String DateFormat_yyyyMMddHHmmssDIS = "yyyy年MM月dd日HH时mm分ss秒";

	public static final String DateFormat_yyyyMMdd = "yyyy年MM月dd日";

	public static String formatORB_EPH(Date date) {
		String ret="";
		Calendar calendar = Calendar.getInstance();;
		calendar.setTime(date);
		// 获取年
		int year = calendar.get(Calendar.YEAR);
		// 获取月，这里需要需要月份的范围为0~11，因此获取月份的时候需要+1才是当前月份值
		int month = calendar.get(Calendar.MONTH) + 1;
		// 获取日
		int day = calendar.get(Calendar.DAY_OF_MONTH);
		// 获取时
//		int hour = calendar.get(Calendar.HOUR);
		 int hour = calendar.get(Calendar.HOUR_OF_DAY); // 24小时表示
		// 获取分
		int minute = calendar.get(Calendar.MINUTE);
		// 获取秒
		int second = calendar.get(Calendar.SECOND);
		ret = new Integer(year).toString() +" "+new Integer(month).toString() +" "+new Integer(day).toString()
				+" "+new Integer(hour).toString()+" "+new Integer(minute).toString()+" "+new Integer(second).toString();
		return ret;
	}

	public static String formatDate(Date date) {
		String ret = "";

		try {
			SimpleDateFormat sdf = new SimpleDateFormat(normalDateFormat);
			ret = sdf.format(date);
		} catch (Exception e) {
		}

		return ret;
	}

	public static boolean isSameMonth(Date date,Date date2) {
		boolean ret = true;

		try {
			SimpleDateFormat sdf = new SimpleDateFormat(normalDateFormatMonth);
			String strdate=  sdf.format(date);
			String strdate2=  sdf.format(date2);
			ret = strdate.equals(strdate2);

		} catch (Exception e) {
		}

		return ret;
	}

	public static String getFormatDate() {
		String ret = "";
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
			ret = sdf.format(new Date());
		} catch (Exception e) {
		}

		return ret;
	}

	public static String getFormatDateTime() {
		String ret = "";
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
			ret = sdf.format(new Date());
		} catch (Exception e) {
		}

		return ret;
	}

	public static String formatTime(Date date) {
		String ret = "";
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(normalTimeFormat);
			ret = sdf.format(date);
		} catch (Exception e) {
		}

		return ret;
	}

	public static String formatDate(Date date, String format) {
		String ret = "";

		try {
			SimpleDateFormat sdf = new SimpleDateFormat(format);
			ret = sdf.format(date);
		} catch (Exception e) {
		}

		return ret;
	}

	// 通过传入的DepartureTime来判断在不在
	// fliterTime里面，在的话返回true，fliterTime格式为00:00-06:00,DepartureTime格式为0000
	public static boolean compareDepartureTime(String DepartureTime,
			String fliterTime) {
		String startTime = fliterTime.substring(0, 2)
				+ fliterTime.substring(3, 5);
		String endTime = fliterTime.substring(6, 8)
				+ fliterTime.substring(9, 11);
		if ((Integer.valueOf(DepartureTime) > Integer.valueOf(startTime))
				&& (Integer.valueOf(DepartureTime) < Integer.valueOf(endTime))) {
			return true;
		}
		return false;

	}

	public static boolean beforeTime(String sTime, String eTime)
			throws ParseException {
		DateFormat df = DateFormat.getDateTimeInstance();
		DateFormat dfe = DateFormat.getDateTimeInstance();
		return df.parse(sTime).before(dfe.parse(eTime));
	}



	/**
	 * 以format格式化str，是否成功
	 * 
	 * @param
	 * @param format
	 * @return
	 */
	public static boolean isDate(String date, String format) {
		DateFormat df = new SimpleDateFormat(format);
		try {
			df.parse(date);
			return true;
		} catch (Exception ex) {
			return false;
		}
	}

	/**
	 * 获取yyyy-MM-dd格式的日期
	 * 
	 * @return String类型 yyyy-mm-dd 2009-01-12
	 */
	public static String getYearMonthDay(Calendar dateTime) {
		int year = dateTime.get(Calendar.YEAR);
		int month = dateTime.get(Calendar.MONTH) + 1;
		int day = dateTime.get(Calendar.DATE);
		return String.valueOf(year) + "-" + String.valueOf(month) + "-"
				+ String.valueOf(day);
	}

	/**
	 * 根据一个日期，返回是星期几的字符串
	 * 
	 * @param date
	 * @return
	 */
	public static int getWeek(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		return c.get(Calendar.DAY_OF_WEEK);
	}

	public static String getWeekStr(Date date) {
		String str = null;
		int day = DateUtil.getWeek(date);
		str = StringUtil.getStr(day);
		if ("0".equals(str)) {
			str = "星期日";
		} else if ("1".equals(str)) {
			str = "星期一";
		} else if ("2".equals(str)) {
			str = "星期二";
		} else if ("3".equals(str)) {
			str = "星期三";
		} else if ("4".equals(str)) {
			str = "星期四";
		} else if ("5".equals(str)) {
			str = "星期五";
		} else if ("6".equals(str)) {
			str = "星期六";
		}
		return str;
	}

	/**
	 * 获取一个星期的第一天
	 * 
	 * @param date
	 * @return
	 * @throws ParseException
	 */
	public static Date getFirstDayOfWeek(Date date) throws ParseException {
		Calendar cDay1 = Calendar.getInstance();
		cDay1.setTime(date);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		final int firstDayOfWeek = cDay1.get(Calendar.DAY_OF_WEEK);
		if (1 == firstDayOfWeek) {
			cDay1.add(Calendar.DAY_OF_MONTH, -1);
		}

		cDay1.setFirstDayOfWeek(Calendar.MONDAY);
		int day = cDay1.get(Calendar.DAY_OF_WEEK);
		cDay1.add(Calendar.DATE, cDay1.getFirstDayOfWeek() - day);
		String imptimeBegin = sdf.format(cDay1.getTime());
		return sdf.parse(imptimeBegin);
	}

	/**
	 * 获取一个月的第一天
	 * 
	 * @param date
	 * @return
	 * @throws ParseException
	 */
	@SuppressWarnings("deprecation")
	public static Date getFirstDayOfMonth(Date date) throws ParseException {
		Calendar cDay1 = Calendar.getInstance();
		cDay1.setTime(date);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		final int firstDay = cDay1.getActualMinimum(Calendar.DAY_OF_MONTH);
		Date firstDate = cDay1.getTime();
		firstDate.setDate(firstDay);
		String c = sdf.format(firstDate);
		return sdf.parse(c);
	}

	public static String getDatePoor(Date endDate, Date nowDate) {

		long nd = 1000 * 24 * 60 * 60;
		long nh = 1000 * 60 * 60;
		long nm = 1000 * 60;
		// long ns = 1000;
		// 获得两个时间的毫秒时间差异
		long diff = endDate.getTime() - nowDate.getTime();
		// 计算差多少天
		long day = diff / nd;
		// 计算差多少小时
		long hour = diff % nd / nh;
		// 计算差多少分钟
		long min = diff % nd % nh / nm;
		// 计算差多少秒//输出结果
		// long sec = diff % nd % nh % nm / ns;
  	return day+ "天" + hour + "小时" + min + "分钟";
	}

	public static long getDatePoorDay(Date endDate, Date nowDate ) {

		long nd = 1000 * 24 * 60 * 60;
		long nh = 1000 * 60 * 60;
		long nm = 1000 * 60;
		// long ns = 1000;
		// 获得两个时间的毫秒时间差异
		long diff = endDate.getTime() - nowDate.getTime();
		// 计算差多少天
		long day = diff / nd;
		// 计算差多少小时
		long hour = diff % nd / nh;
		// 计算差多少分钟
		long min = diff % nd % nh / nm;
		// 计算差多少秒//输出结果
		// long sec = diff % nd % nh % nm / ns;

			return day;

	}

	/** 生成日期解析对象
	 * @Param: pattern:日期格式，如yyyy-MM-dd HH:mm:ss
	 * @return: DateFormat 日期解析对象
	 * @Author: longwj
	 * @Date: 2018/9/3 14:44
	 */
	private static DateFormat doDateFormat(String pattern){
		return new SimpleDateFormat(pattern);
	}

	/**
	 *@Description: 字符串转换到日期
	 * @Param: dateStr: 日期字符串,pattern: 日期格式，如yyyy-MM-dd HH:mm:ss
	 * @return: 转换后的日期
	 * @Author: longwj
	 * @Date: 2018/9/3 14:09
	 */
	public static Date stringToDate(String dateStr, String pattern){
		Date date = null;
		try{
			date = doDateFormat(pattern).parse(dateStr);
		}catch (Exception e){
			e.printStackTrace();
		}
		return date;
	}

	/**
	 *@Description: 日期转换成字符串
	 * @Param: paramDate:要转换的日期，pattern:日期格式，如yyyy-MM-dd HH:mm:ss
	 * @return: 日期字符串
	 * @Author: longwj
	 * @Date: 2018/9/3 15:17
	 */
	public static String dateToString(Date paramDate, String pattern){
		return doDateFormat(pattern).format(paramDate);
	}

	/**
	 *@Description: 特定时间加减特定秒数之后结果
	 * @Param:
	 * 	dateStr: 计算前时间字符串
	 * 	pattern: 时间格式，如yyyy-MM-dd HH:mm:ss
	 * 	seconds: 要计算的秒数(正为加，负为减)
	 * @return: 时间结果字符串
	 * @Author: longwj
	 * @Date: 2018/9/3 17:52
	 */
	public static String addSeconds(String dateStr,String pattern,int seconds){
		Date date = stringToDate(dateStr,pattern);
		Calendar specialDate = Calendar.getInstance();
		specialDate.setTime(date);
		specialDate.add(Calendar.SECOND, seconds);
		Date dateAffter = specialDate.getTime();
		return dateToString(dateAffter,pattern);
	}

	/**
	 *@Description: 格式化升交点和降交点的时间格式，
	 * 类似" 2016  9  1 11 47      37.2497"格式，改为"yyyy-MM-dd HH:mm:ss"
	 * @Param: 格式化前时间字符串
	 * @return: 格式化后时间字符串
	 * @Author: longwj
	 * @Date: 2018/9/3 17:57
	 */
	public static String formatASNSNodeDateTime(String timeStr){
		String formateTime = "";
		String[] arr = timeStr.trim().split("\\s+");
		for(int i=1;i<arr.length-1;i++){
			if(Integer.parseInt(arr[i])<10){
				arr[i]="0"+arr[i];
			}
		}
		//秒数有小数
		if(Float.parseFloat(arr[5])<10){
			arr[5] = "0" + arr[5];
		}
		formateTime = arr[0].trim()+"-"+arr[1]+"-"+arr[2]+" "+arr[3]+":"+arr[4]+":"+arr[5];
		return formateTime;
	}

	/**
	 *@Description: 比较两个时间大小(yyyy-MM-dd HH:mm:ss格式)
	 * @Param: paramDate1,paramDate2
	 * @return: boolean
	 * @Author: longwj
	 * @Date: 2018/9/9 9:50
	 */
	public static boolean chceckDateOfcurrentDate(String paramDate1,
												  String paramDate2) {
		Date date1 = stringToDate(paramDate1, "yyyy-MM-dd HH:mm:ss");
		Date date2 = stringToDate(paramDate2, "yyyy-MM-dd HH:mm:ss");
		Long diffTimes = date1.getTime() - date2.getTime();
		return diffTimes > 0 ? true : false;
	}
	public static void main(String[] args) throws ParseException {

		Date now = new Date();

		Calendar c = Calendar.getInstance();
		c.setTime(now);

		Calendar cal = c;//Calendar.getInstance();
		cal.set(Calendar.DAY_OF_MONTH, 1);
////日期减一,取得上月最后一天时间对象
		cal.add(Calendar.DAY_OF_MONTH, -1);

		String nowDay = StringUtil.DateToStr(cal.getTime(), "yyyyMM");

		String xx = StringUtil.DateToStr(cal.getTime(), "yyyyMMdd");
		DateUtil dt = new DateUtil();
//		System.out.println(dt.getDatePoor(Calendar.getInstance().getTime(),dt.stringToDate("2018-05-30",normalDateFormat)));
//		System.out.println(DateUtil.beforeTime("2009-02-16 17:21:05.58",
//				"2009-02-16 17:21:05.159"));

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//		Date date = null;
//		try {
//			date = sdf.parse("2018-05" + "-" + "05");
//		} catch (ParseException e) {
//			e.printStackTrace();
//		}
//
//
//		Calendar c = Calendar.getInstance();
//		c.setTime(date);
//		System.out.println(isSameMonth(c.getTime(),new Date()));
//		//取得系统当前时间
////		Calendar cal = Calendar.getInstance();
//		Calendar cal = c;//Calendar.getInstance();
//////取得系统当前时间所在月第一天时间对象
//		cal.set(Calendar.DAY_OF_MONTH, 1);
//////日期减一,取得上月最后一天时间对象
//		cal.add(Calendar.DAY_OF_MONTH, -1);
////输出上月最后一天日期
//
////		c.setTime(date);
//
//		System.out.println(isSameMonth(c.getTime(),new Date()));
//
//		System.out.println(cal.get(Calendar.DAY_OF_MONTH));
//		for (int i=3;i<=cal.get(Calendar.DAY_OF_MONTH);i++)
//		{
//            String month = StringUtil.getStr(cal.get(Calendar.MONTH)+1);
//            if (month.length()<2) {
//                month = "0" + month;
//            }
//			System.out.println(cal.get(Calendar.YEAR) + "-"	+ (cal.get(Calendar.MONTH)+1 + "-"+i));
//            String day = StringUtil.getStr(i);
//            if (day.length()<2) {
//                day = "0" + day;
//            }
//            System.out.println(cal.get(Calendar.YEAR) + "-"	+ month + "-"+day);
//		}
//



	}
}
